package com.example.farmer.Presenter;

import com.example.farmer.Database.PurchasePublishDatabase;
import com.example.farmer.View.Purchase.PurchasePostView;

public class PurchasePostPresenter {
    private PurchasePostView view;
    private PurchasePublishDatabase purchasePublishDatabase;

    public PurchasePostPresenter(PurchasePostView mview) {
        view = mview;
        purchasePublishDatabase = new PurchasePublishDatabase(view.getContext());
    }

    public void posts(String pushtype,String producttype,String productname,  String need, String shipaddress,
                      String period,String shippingaddress,String describe)
    {
        view.showLoading();
        if (getValidationErrorMessage(pushtype,producttype,productname,need,shipaddress,
                period,shippingaddress, describe)) {
            // 验证成功，执行发布逻辑
            long result = purchasePublishDatabase.insertPost(pushtype,producttype,productname,need,shipaddress,
                    period,shippingaddress, describe);
            if (result !=-1) {
                view.hideLoading();
//                List<ClassPost> posts = post_db.getPostsByType("/* 相应的类型 */");
                view.showPostSuccess();
            } else {
                view.hideLoading();
                view.showPostError("数据库插入失败，请稍后重试。");
            }
        } else {
            view.hideLoading();
            // 验证失败，显示所有错误信息
            view.showPostError("内容验证失败");
        }
    }


    private boolean getValidationErrorMessage(String pushtype,String producttype,String productname,  String need, String shipaddress,
                                              String period,String shippingaddress,String describe) {
        if (productname.length() < 2 || productname.length() > 15) {
            view.hideLoading();
            view.showPostError("货品名称必须在2到15个字符之间。\n");
            return false;
        }
        if (productname.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("货品名称不能为空。\n");
            return false;
        }
        if (need.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("需求量不能为空。\n");
            return false;
        }
        if (shipaddress.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("发货地址不能为空。\n");
            return false;
        }
        if (period.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("采购周期不能为空。\n");
            return false;
        }
        if (shippingaddress.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("发货地址不能为空。\n");
            return false;
        }

        if (pushtype.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("请选择发布类型。\n");
            return false;
        }

        if (producttype.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("请选择货品类型。\n");
            return false;
        }


        // 检查内容是否只包含数字、字母、汉字和标点符号
        if (!isValidContent(productname,shipaddress,shippingaddress,period,describe)) {
            // 内容包含无效字符，发布失败并说明理由
            view.hideLoading();
            view.showPostError("内容只能包含数字、字母、汉字和标点符号");
            return false;
        }
        return isValidContent(productname,shipaddress,shippingaddress,period,describe);
    }


    private boolean isValidContent(String productname,  String need, String shipaddress,
                                   String period,String shippingaddress) {
        String regex = "^[\u4e00-\u9fa5a-zA-Z0-9,.!?;，。:：“‘() ]+$";
        return productname.matches(regex)||shipaddress.matches(regex)||need.matches(regex)
                ||period.matches(regex)||shippingaddress.matches(regex);
    }
}
